package com.ruoyi.wechat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.wechat.common.Constants;
import com.ruoyi.wechat.domain.DictData;
import com.ruoyi.wechat.mapper.DictDataMapper;
import com.ruoyi.wechat.service.IDictDataService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 全局字典接口实现
 * </p>
 *
 * @author YinHeng
 * @since 2024/3/3
 */
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements IDictDataService {
    @Override
    public Map<String, List<DictData>> dictMap() {
        //先从缓存中取，没有再查数据库
        LambdaQueryWrapper<DictData> query = new LambdaQueryWrapper<>();
        query.eq(DictData::getStatus, Constants.Common.STATUS_0.getValue().toString())
                .orderByAsc(DictData::getDictSort);
        List<DictData> list = this.list(query);
        Map<String, List<DictData>> dictMap = new HashMap<>();

        list.forEach(item -> {
            List<DictData> type = dictMap.get(item.getDictType());
            if (ObjectUtils.isEmpty(type)) {
                type = new ArrayList<>();
            }
            type.add(item);
            dictMap.put(item.getDictType(), type);
        });
        return dictMap;
    }
}
